package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{

   @Reference
    private RoleService roleService;
   @Reference
   private PermissionService permissionService;

/*    @RequestMapping
    public String findAll(Model model) {

        List<Role> list = roleService.findAll();

        model.addAttribute("list", list);

        return "role/index";
    }*/
    @RequestMapping
    public String findPage (HttpServletRequest request,Model model) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Role> pageInfo = roleService.findPage(filters);

        model.addAttribute("page", pageInfo);
        model.addAttribute("filters",filters);

        return "role/index";
    }

    @RequestMapping("create")
    public String create() {
        return "role/create";
    }

    /* @RequestMapping("save")
     public String save(Role role){
         roleService.save(role);
         return "redirect:/role";
     }*/
    /*保存用户*/
    @PostMapping("/save")
    public String save(Role role, HttpServletRequest request) {
        roleService.insert(role);
        return "common/successPage";
    }

    /*修改用户
     * 获取用户*/
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    /*根据获取到的用户修改*/
    @PostMapping("/update")
    public String update(Role role, Model model) {
        roleService.update(role);
        return "common/successPage";
    }
    /*根据ID删除用户*/
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        roleService.delete(id);
        return "redirect:/role";
    }

    /**
     * 封装页面提交的分页参数及搜索条件
     * @param request
     * @return
     */
    protected Map<String, Object> getFilters(HttpServletRequest request) {
        Enumeration<String> paramNames = request.getParameterNames();
        Map<String, Object> filters = new TreeMap();
        while(paramNames != null && paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            if (values != null && values.length != 0) {
                if (values.length > 1) {
                    filters.put(paramName, values);
                } else {
                    filters.put(paramName, values[0]);
                }
            }
        }
        if(!filters.containsKey("pageNum")) {
            filters.put("pageNum", 1);
        }
        if(!filters.containsKey("pageSize")) {
            filters.put("pageSize", 5);
        }

        return filters;
    }


    /*分配权限，跳转页面，需要roleId 以及 permissionIds */
    @RequestMapping("assignShow/{id}")
    public String assign(@PathVariable("id") Long roleId ,Model model){
        /*链表查询，通过roleId查询出对应的permission*/
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("roleId",roleId);
        model.addAttribute("zNodes",zNodes);

        return "role/assignShow";
    }

    @RequestMapping("assignPermission")
    public String assignPermission (Long roleId,Long [] permissionIds){
            permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);

            return "common/successPage";
    }


}
